package com.sg.business.model.dataset.project;

import java.util.ArrayList;
import java.util.List;

import com.mobnut.db.model.DataSet;
import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.ProjectProgressStep;
import com.sg.widgets.commons.dataset.MasterDetailDataSetFactory;

public class ProjectWeeklySteps extends MasterDetailDataSetFactory {

	public ProjectWeeklySteps(String dbName, String collectionName) {
		super(IModelConstants.DB, IModelConstants.C_PROJECT_PROGRESS_STEPS);
	}

	@Override
	protected String getDetailCollectionKey() {
		return "parent_id";
	}

	@Override
	public DataSet getDataSet() {
		return new DataSet(getQuery());
	}

	public List<PrimaryObject> getQuery() {
		List<PrimaryObject> ds = new ArrayList<PrimaryObject>();

		Object value = master.getValue(getDetailCollectionKey());

		DBCollection collection = getCollection();
		DBCursor cursor = collection.find(new BasicDBObject().append(
				ProjectProgressStep.F_PARENT_ID, value));
		while (cursor.hasNext()) {
			DBObject dbo = cursor.next();
			ds.add(ModelService.createModelObject(dbo,
					ProjectProgressStep.class));
		}
		cursor.close();
		return ds;
	}

}
